package com.ykdl.member.kid.adapters;




import java.util.List;

import com.ykdl.member.kid.R;
import com.ykdl.member.base.QLog;
import com.ykdl.member.kid.gears.HomeActivity;
import com.ykdl.member.kid.image.ImageOptions;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.models.AdItemBean;
import com.ykdl.member.kid.models.DownloadUrls;
import com.ykdl.member.kid.models.FileMetas;
import com.ykdl.member.kid.models.ImageInfo;
import com.ykdl.member.kid.util.MobileUtils;
import com.ykdl.member.kid.widget.AsyncImageView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;

public class ViewFlowAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private Context  context;
	private List<AdItemBean> adItems = null;
	private int screen_width = 0;
	private int image_height = 0;
	public ViewFlowAdapter(Context context, List<AdItemBean> adItems) {
		this.context = context;
		this.adItems = adItems;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		screen_width = MobileUtils.getScreenWidthIntPx();
		image_height = (int)(screen_width / 2.3);
		
	}

	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
	}

	@Override
	public Object getItem(int position) {
		return adItems !=null && adItems.size() > 0 ? adItems.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		HoldView  holdView = null;
		if (convertView == null) {
			holdView = new HoldView();
			convertView = mInflater.inflate(R.layout.viewflow_image_item, null);
            AsyncImageView  iv = (AsyncImageView) convertView.findViewById(R.id.imgView);
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, MobileUtils.dpToPx((int)(image_height / 1.5)));
			iv.setLayoutParams(params);
//            android.widget.RelativeLayout.LayoutParams  params = (android.widget.RelativeLayout.LayoutParams) iv.getLayoutParams();
//            params.width = android.widget.RelativeLayout.LayoutParams.FILL_PARENT;
//            params.height = MobileUtils.dpToPx((int)(image_height / 1.5));
			holdView.imageview = iv;
			convertView.setTag(holdView);
			
		}else{
			holdView = (HoldView) convertView.getTag();
		}
		
		
		if(adItems != null && adItems.size() > 0){
//			AdItemBean item = adItems.get(position % adItems.size());
//			FileMetas meta = item.getFilemeta();
//			DownloadUrls down = meta.getDownload_urls();
//			ImageInfo info = down.getOrigin();
//			String url = info.getUrl();
			int index = position % adItems.size();
			String url = adItems.get(index).getFile_meta().getDownload_urls().getBig().getUrl();
			//网络请求图片
			ImageOptions options = ImageOptionsFactory.getBigPortraitOptions(context);
			options.setHeight(MobileUtils.pxToDp(204));
			options.setRatio(1.0f);
			holdView.imageview.setOnClickListener(new onadItemClick(index));
			holdView.imageview.setUrl(((HomeActivity)context).TAG, url, options);
		}

		return convertView;
	}
	
	public class HoldView{
		public AsyncImageView  imageview;
	}

	class onadItemClick implements OnClickListener{
		private int position;
		public onadItemClick(int position){
			this.position = position;
		}
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			viewFlowOnItemClick(position);
		}
		
	}
	public void viewFlowOnItemClick(int position) {
		final AdItemBean  item = this.adItems.get(position);
		((HomeActivity)context).goAction(item.getUrl());
	}
}
